package cn.tedu.egweibo0416.controller;

import cn.tedu.egweibo0416.base.response.JsonResult;
import cn.tedu.egweibo0416.mapper.WeiboMapper;
import cn.tedu.egweibo0416.pojo.dto.WeiboAddParam;
import cn.tedu.egweibo0416.pojo.entity.Weibo;
import cn.tedu.egweibo0416.pojo.vo.UserVO;
import cn.tedu.egweibo0416.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo0416.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Validated
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
@Api(tags = "02.微博功能模块")
public class WeiboController {
    @Autowired
    WeiboMapper weiboMapper;
    @PostMapping("insert")
    @ApiOperation(value="发布微博")
    @ApiOperationSupport(order=10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, @ApiIgnore HttpSession session){
        log.debug("weiboAddParam = "+weiboAddParam);
        UserVO userVO= (UserVO)session.getAttribute("user");
       /* if(userVO==null){
            return new JsonResult(Status.NOT_LOGIN);
        }*/
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreateTime(new Date());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }
    @GetMapping("selectIndex")
    @ApiOperation(value="首页微博列表")
    @ApiOperationSupport(order=20)
    public JsonResult selectIndex(){
        List<WeiboIndexVO> weiboIndexVOS= weiboMapper.selectIndex();
        return JsonResult.ok(weiboIndexVOS);
    }
    @GetMapping("selectById")
    @ApiOperation(value="微博详情页")
    @ApiOperationSupport(order=30)
    public JsonResult selectById(@Range(min=100,max=500,message="微博编号必须在100-500之间") @RequestParam Integer id){
        WeiboDetailVO weiboDetailVO=weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}
